<template>
    <div>
        <h1>添加书籍</h1>
        <br>
        书籍名称：<input type="text" v-model="title">
        <br>
        书籍内容：<input type="text" v-model="content">
        <br>
        作者：
        <select name="" id="" v-model="author_id">
            <option :value="i.id" v-for="i in authorlist">{{i.name}}</option>
        </select>
        <br>
        <input type="button" @click="addbooks" value="添加">
        <br>
    </div>
</template>

<script>
export default {
    data:function(){
        return{
            title:'',
            content:'',
            author_id:0,
            authorlist:[]
        }
    },
    methods:{
        init: function(){
            this.axios({
                url: "http://127.0.0.1:8000/authorlist/",
                method: "post",
            }).then((res)=>{
                if(res.data.code==200)
                {
                    this.authorlist = res.data.message
                }
            })
        },
        addbooks:function(){
            let formData = new FormData();
            formData.append('title', this.title);
            formData.append('content', this.content);
            formData.append('author', this.author_id);
            // formData.append('name', this.name);

            this.axios({
                url: "http://localhost:8000/addbooks/",
                method: "post",
                data: formData,
            }).then((res)=>{
                if(res.data.code==200)
                {
                    alert("添加成功"),
                    location.reload()
                }
                else
                {
                    // alert("添加失败")
                    alert(res.data.message)
                }
            })
        }
    },
    mounted(){
        this.init()
    }
}
</script>
